import 'package:boss/ui/page/booking/bookingHistory/page/bookingSearch/booking_search_page.dart';
import 'package:boss/ui/widget/center_title_app_bar.dart';
import 'package:boss/ui/widget/label_button.dart';
import 'package:flutter/material.dart';
import 'package:hi_common/hi_common.dart';
import 'package:provider/provider.dart';
import 'package:router/annotation.dart';
import 'provider/booking_history_provider.dart';
import 'widget/booking_history_browser.dart';

@RouterConfig(BookingHistoryPage.pageName)
class BookingHistoryPage extends StatefulWidget {
  static const String pageName = '/BookingHistoryPage';

  @override
  _BookingHistoryPageState createState() => _BookingHistoryPageState();
}

class _BookingHistoryPageState extends BaseState<BookingHistoryPage> {
  _BookingHistoryPageState() : super(BookingHistoryPage.pageName);

  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider(
      create: (_) => BookingHistoryProvider(),
      child: Builder(
          builder: (context) => Scaffold(
                appBar: CenterTitleAppBar(
                  title: '预订信息',
                  actions: <Widget>[
                    LabelButton(
                      title: '高级查询',
                      padding: EdgeInsets.only(left: 8, top: 2, bottom: 2),
                      onTap: () async {
                        final provider = Provider.of<BookingHistoryProvider>(context, listen: false);

                        final res = (await Navigator.pushNamed(context, BookingSearchPage.pageName, arguments: {
                          'startTime': provider.startTime,
                          'endTime': provider.endTime,
                          'beginArrive': provider.beginArriveDateTime,
                          'endArrive': provider.endArriveDateTime,
                          'status': provider.status,
                          'titleEnum': provider.titleEnum
                        })) as Map<String, dynamic>;

                        if (res != null) {
                          provider.setAll(
                            startTime: res['startTime'],
                            endTime: res['endTime'],
                            beginArrive: res['beginArrive'],
                            endArrive: res['endArrive'],
                            status: res['status'],
                            titleEnum: res['titleEnum'],
                          );
                          provider.refreshKey.currentState.refresh();
                        }
                      },
                    ),
                  ],
                ),
                body: BookingHistoryBrowser(),
              )),
    );
  }
}
